package com.sanpu.kxe.business.main.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseFragment;
import com.sanpu.kxe.business.main.adapter.NewsContentAdapter;
import com.sanpu.kxe.business.main.bean.NewsContentOutput;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.business.main.contract.NewsColumnContract;
import com.sanpu.kxe.business.main.presenter.NewsColumnPresenter;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.module.widget.VerticalSwipeRefreshLayout;
import com.sanpu.kxe.utils.ScreenUtil;

import java.util.List;

import butterknife.Bind;

/**
 * Description：新闻列表页
 * Created by 薛勇军 on 2018/8/29.
 */

public class NewsColumnFragment extends BaseFragment implements NewsColumnContract.View {

    @Bind(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @Bind(R.id.newsRv)
    RecyclerView newsRv;
    @Bind(R.id.emptyView)
    View emptyView;

    private NewsContentAdapter mContentAdapter;

    private int nowPage = 0;

    private String mCurrentType;

    private NewsColumnContract.Presenter mPresenter;

    public static NewsColumnFragment getInstance(String columnType) {
        NewsColumnFragment fragment = new NewsColumnFragment();
        Bundle bundle = new Bundle();
        bundle.putString(MainConfig.INTENT_COLUMN_TYPE, columnType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCurrentType = (String) getArguments().get(MainConfig.INTENT_COLUMN_TYPE);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_news_column;
    }

    @Override
    protected void initView() {
        super.initView();
        int mViewGroupWidth = 0;
        try {
            WindowManager manager = getActivity().getWindowManager();
            DisplayMetrics outMetrics = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(outMetrics);
            mViewGroupWidth = outMetrics.widthPixels;
            mViewGroupWidth = (mViewGroupWidth- ScreenUtil.dp2px(30))/3;
        } catch (Exception e) {
            e.printStackTrace();
        }
        newsRv.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        mContentAdapter = new NewsContentAdapter(mViewGroupWidth);
        newsRv.setAdapter(mContentAdapter);
        mContentAdapter.disableLoadMoreIfNotFullPage(newsRv);
        mContentAdapter.setPreLoadNumber(2);
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter = new NewsColumnPresenter(this);
        mPresenter.getNewsList(nowPage, mCurrentType);
    }

    @Override
    protected void setListener() {
        super.setListener();
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = 0;
                mPresenter.getNewsList(nowPage, mCurrentType);
                mContentAdapter.setEnableLoadMore(true);
            }
        });
        mContentAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getNewsList(nowPage, mCurrentType);
            }
        }, newsRv);
        mContentAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mPresenter.OnItemClick(((NewsContentAdapter)adapter).getItem(position));
            }
        });
    }

    @Override
    public void showNewsListView(List<NewsContentOutput> contentOutputs, boolean isFirst) {
        if (contentOutputs != null && contentOutputs.size()>0) {
            vSwipeRefreshLayout.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
            if (isFirst) {
                mContentAdapter.setNewData(contentOutputs);
            } else {
                mContentAdapter.addData(contentOutputs);
            }
            nowPage++;
        } else if (isFirst){
            vSwipeRefreshLayout.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                mContentAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                mContentAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                mContentAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(NewsColumnContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
